package com.bocloud.cmp.server.controller.stats;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.interfaces.stats.PlatformService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;

@RestController
@RequestMapping("/platform")
public class PlatformController {

	@Autowired
	private PlatformService platformService;

	/**
	 * 得到平台的概要信息
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/abstracts", method = { RequestMethod.GET })
	public BsmResult abstracts(@RequestParam(value = Common.PARAMS, required = false) String params) {
		return platformService.getAbstracts(params);
	}

	/**
	 * 得到平台的宿主机列表
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param type
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hosts", method = { RequestMethod.POST })
	public BsmResult hosts(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.TYPE, required = false) String type, @Value(Common.REQ_USER) RequestUser user)
					throws Exception {
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return platformService.getHosts(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, type);
	}

	/**
	 * 得到平台的告警列表
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param type
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/alarms", method = { RequestMethod.POST })
	public BsmResult alarms(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.TYPE, required = false) String type, @Value(Common.REQ_USER) RequestUser user)
					throws Exception {
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return platformService.getAlarms(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, type);
	}

	/**
	 * 得到平台的概要信息
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/export", method = { RequestMethod.GET })
	public BsmResult export(@RequestParam(value = Common.PARAMS, required = false) String params) {
		return platformService.export(params);
	}

}